//
//  XQTask.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/6/1.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation
import Cocoa


class XQProcess: Process {
    
    class func restartToFile(fileName:String,basicPath:String?=nil)  {
        self.save()
        self.openFile(fileName: fileName, basicPath: basicPath)
        exit(0)
    }
    
    class func restart()  {
        self.save()
        self.open()
        exit(0)
    }
    
    class func save() {
        
    }
    
    class func open() {
        let path = Bundle.main.bundlePath
        let task = Process()
        task.launchPath = "/usr/bin/open"
        task.arguments = ["-n",path]
        task.launch()
    }
    
    @discardableResult
    class func openFile(fileName:String,basicPath:String?=nil)->String? {
        var bpath = basicPath
        if basicPath == nil {
            let paths = NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true)
            bpath = paths[0]
        }
        let urlPath = bpath!+"/"+fileName
        
        let task = Process()
        task.launchPath = "/usr/bin/open"
        task.arguments = [urlPath]
        task.terminationHandler = { proce in
            print("finish")
        }
        let pipe = Pipe()
        task.standardOutput = pipe
        let fileHandle = pipe.fileHandleForReading
        
        task.launch()
        task.waitUntilExit()
        let log = String.init(data: fileHandle.readDataToEndOfFile(), encoding: String.Encoding.utf8)
        return log
    }
    
}
